package dining;

import javax.swing.*;
import java.util.Date;

public class Test {
    public static String[] idArr = {"zhangsan", "lisi"};
    public static String[] passwordArr = {"123", "456"};

    public static double[] accountdArr = {500, 1000}; // 余额
    public static double[] totalArr = {100, 200}; // 累计消费

    public static String[] logArr = new String[10]; // 日志，用于记录每次操作

    public static int userIndex = -1;

    public static void main(String[] args) {
//        String opt = JOptionPane.showInputDialog(null, "欢迎，请选择：\n 1. 登录 \n 2. 退出：");
//        if (opt != null) {
//            switch (opt) {
//                case "1":
//                    userIndex = login();
//                    JOptionPane.showMessageDialog(null, "欢迎你" + idArr[userIndex]);
//                    break;
//                case "2":
//                    JOptionPane.showMessageDialog(null, "欢迎下次使用");
//                    break;
//            }
//        }
        userIndex = 0; // 假设当前是 zhangsan 登录
        // 登录成功
        if (userIndex != -1) {
            while (true) {
                String opt = JOptionPane.showInputDialog(null, "请选择：\n 1. 充值 \n 2. 消费 \n 3. 余额 \n 4. 交易记录 \n 5. 修改密码 \n 6. 退出：");
                if (opt == null) {
                    return;
                }
                switch (opt) {
                    case "1": // 充值
                        deposit();
                        break;
                    case "2": // 消费
                        consume();
                        break;
                    case "3": // 余额查询
                        JOptionPane.showMessageDialog(null, "当前余额：" + accountdArr[userIndex] + "\n" + "累计消费" + totalArr[userIndex]);
                        break;
                    case "4": // 交易记录
                        queryHistory();
                        break;
                    case "5": // 修改密码
                        changePassword();
                        break;
                    case "6": // 退出
                        JOptionPane.showMessageDialog(null, "886");
                        break;
                }
            }

        }
    }

    // 修改密码
    public static void changePassword() {
        String password = JOptionPane.showInputDialog(null, "请输入旧密码：");
        if (password.equals(passwordArr[userIndex])) {
            String newPasswrod = JOptionPane.showInputDialog(null, "请输入新密码");
            String reNewPasswrod = JOptionPane.showInputDialog(null, "请再次输入新密码");
            if (newPasswrod.equals(reNewPasswrod)) {
                passwordArr[userIndex] = newPasswrod;
                login();
            } else {
                JOptionPane.showMessageDialog(null, "密码不一致");
            }

        } else {
            JOptionPane.showMessageDialog(null, "密码不一致");
        }
    }

    // 查询历史记录
    public static void queryHistory() {
        String str = "交易信息    日期 \n";
        for (int i = 0; i < logArr.length; i++) {
            if (logArr[i] != null) {
                str += logArr[i] + "\n";
            }
        }
        JOptionPane.showMessageDialog(null, str);
    }

    // 消费
    public static void consume() {
        String value = JOptionPane.showInputDialog(null, "请输入消费金额：");
        int money = Integer.parseInt(value);
        if (money < accountdArr[userIndex]) {
            accountdArr[userIndex] -= money; // 余额减少
            totalArr[userIndex] += money; // 消费金额累计
            if (totalArr[userIndex] > 1000) {
                accountdArr[userIndex] += 50; // 返利 50
                totalArr[userIndex] -= 1000; // 累计消费减 1000
                System.out.println("");
                JOptionPane.showMessageDialog(null, "本次消费:" + money + "元 \n 返利 50 元 \n 累计消费：" + totalArr[userIndex] + "元  \n 余额：" + accountdArr[userIndex]);
            } else {
                JOptionPane.showMessageDialog(null, "本次消费:" + money + "元 \n 累计消费：" + totalArr[userIndex] + "元，余额：" + accountdArr[userIndex]);
            }
        } else {
            JOptionPane.showMessageDialog(null, "余额不足");
        }
    }

    // 充值
    public static void deposit() {
        String money = JOptionPane.showInputDialog(null, "请输入充值金额：");
        accountdArr[userIndex] += Integer.parseInt(money);
        JOptionPane.showMessageDialog(null, "充值成功，本次充值:" + money + "元，\n 卡上余额：" + accountdArr[userIndex]);

        String logInfo = "充值" + money + "元     " + getDate();
        update(logInfo);
    }

    // 获取时间
    public static String getDate() {
        Date date = new Date();
        int year = date.getYear() + 1900,
                month = date.getMonth() + 1,
                day = date.getDate();
        return year + "-" + month + "-" + day;
    }

    // 保存日志信息进数组
    public static void update(String logInfo) {
        for (int i = 0; i < logArr.length; i++) {
            if (logArr[i] == null) {
                logArr[i] = logInfo;
                break;
            }
        }
    }


    // 登录
    public static int login() {
        String username = JOptionPane.showInputDialog(null, "请输入账号：");
        String password = JOptionPane.showInputDialog(null, "请输入密码：");
        for (int i = 0; i < idArr.length; i++) {
            if (username.equals(idArr[i]) && password.equals(passwordArr[i])) {
                return i;
            }
        }
        return -1;
    }
}












